package com.gomtel.smartdevice.server;


import android.app.Service;
import android.bluetooth.BluetoothGatt;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleGattCallback;
import com.clj.fastble.callback.BleScanCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.exception.BleException;
import com.gomtel.smartdevice.bean.BuleToothContent;
import com.gomtel.smartdevice.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2018/4/2.
 */

public class DeviceConnectionService extends Service {

    protected String TAG = getClass().getSimpleName();
    private IntentFilter intentFilter;
    private DeviceConnectionReceiver deviceConnectionReceiver;
    private String content = "";
    private ArrayList<BuleToothContent> BuleToothContentList = new ArrayList<>();

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        deviceRegisterReceicer();
        startScan();
        return super.onStartCommand(intent, flags, startId);

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void startScan() {
        BleManager.getInstance().scan(new BleScanCallback() {
            @Override
            public void onScanStarted(boolean success) {
                LogUtil.e(TAG, "onScanStarted : " + success);
            }

            @Override
            public void onLeScan(BleDevice bleDevice) {
                super.onLeScan(bleDevice);
                if (!content.contains(bleDevice.getName())) {
                    content = content + bleDevice.getName();
                    Intent intent = new Intent();
                    intent.setAction("com.gomtel.smartdevice.connection");
                    BuleToothContent buleToothContent = new BuleToothContent(bleDevice.getName(), bleDevice.getMac());
                    BuleToothContentList.add(buleToothContent);
                    intent.putExtra("BuleToothContent", buleToothContent);
                    sendBroadcast(intent);
                }
            }

            @Override
            public void onScanning(BleDevice bleDevice) {
                LogUtil.e(TAG, "BleDevice : " + bleDevice);
            }

            @Override
            public void onScanFinished(List<BleDevice> scanResultList) {
                LogUtil.e(TAG, "scanResultList : " + scanResultList);
            }
        });
    }

    public void connect(final BleDevice bleDevice) {
        BleManager.getInstance().connect(bleDevice, new BleGattCallback() {
            @Override
            public void onStartConnect() {
                LogUtil.e(TAG, "onStartConnect");
            }

            @Override
            public void onConnectFail(BleDevice bleDevice, BleException exception) {
                LogUtil.e(TAG, "onConnectFail : " + exception);
            }

            @Override
            public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {
                LogUtil.e(TAG, "onConnectSuccess : " + status);
            }

            @Override
            public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, BluetoothGatt gatt, int status) {
                LogUtil.e(TAG, "onDisConnected : " + status);
            }
        });
    }

    private void deviceRegisterReceicer() {
        try {
            intentFilter = new IntentFilter();
            intentFilter.addAction("com.gomtel.smartdevice.connection");
            deviceConnectionReceiver = new DeviceConnectionReceiver();
            registerReceiver(deviceConnectionReceiver, intentFilter);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 广播接收者
     */
    class DeviceConnectionReceiver extends BroadcastReceiver {
        public DeviceConnectionReceiver() {
        }

        @Override
        public void onReceive(Context context, final Intent intent) {
            String ConnectionIndex = intent.getStringExtra("ConnectionIndex");

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
